home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / gfx / conv / gfx2grob.lha / gfx2grob / src / FileStuf.c < prev    next >
C/C++ Source or Header  |  1994-11-20  |  3KB  |  110 lines

  1. /**************************************************************************
  2.  
  3.              Open, close & test files
  4.  
  5.        Copyright (C) 1994 by Alexandros Loghis,  All Rights Reserved
  6.  
  7. **************************************************************************/
  8.  
  9. #include <stdio.h>    /* gets, fopen, fclose */
  10. #include <stdlib.h>    /* exit */
  11. #include <ctype.h>    /* toupper */
  12. #include <string.h>    /* strcpy, strcat, strrchr, strcmp */
  13.  
  14. #include "gfx2grob.h"
  15. #include "PrintMsg.h"
  16. #include "FileStuf.h"
  17. #ifdef AMIGA
  18. #include "IffStuff.h"
  19. #endif
  20.  
  21.  
  22. static void MakeOutputFileName(OptionsType *Options);
  23. static void TestFileExists(char *FName);
  24. static void CloseFile(FILE **File, char **FName);
  25.  
  26. /*************************************************************************/
  27.  
  28. static void MakeOutputFileName(OptionsType *Options)
  29. {
  30.   char    *pChar;
  31.  
  32.  
  33.   strcpy(Options->NewOutputfileName, Options->InputfileName);
  34.   if (pChar = strrchr(Options->NewOutputfileName, '.')) *pChar = '\0';
  35.   switch (Options->Option) {
  36.  
  37.     case OT_TOASCII :
  38.       pChar = ".src";
  39.     break;
  40.  
  41.     case OT_TOBIN :
  42.       pChar = ".raw";
  43.     break;
  44.  
  45. #ifdef AMIGA
  46.     case OT_TOIFF :
  47.       pChar = ".pic";
  48.     break;
  49. #endif
  50.   }
  51.   strcat(Options->NewOutputfileName, pChar);
  52.   Options->OutputfileName = Options->NewOutputfileName;
  53.   PrintMsg(MSG_OUTFNAM_S, Options->NewOutputfileName);
  54. }
  55.  
  56. /*************************************************************************/
  57.  
  58. extern void OpenFiles(gvType *gv, char *OpenMode, char *WriteMode)
  59. {
  60.   if (!(gv->Inputfile = fopen(gv->Options.InputfileName, OpenMode)))
  61.     PrintMsg(ERR_OPEN_S, gv->Options.InputfileName);
  62.   if (!*gv->Options.OutputfileName)
  63.     MakeOutputFileName(&gv->Options);
  64.   TestFileExists(gv->Options.OutputfileName);
  65.   if (!(gv->Outputfile = fopen(gv->Options.OutputfileName, WriteMode)))
  66.     PrintMsg(ERR_OPEN_S, gv->Options.OutputfileName);
  67. }
  68.  
  69. /*************************************************************************/
  70.  
  71. static void TestFileExists(char *FName)
  72. {
  73.   FILE *FilePtr;
  74.  
  75.   char Answer[4];
  76.  
  77.  
  78.   if (FilePtr = fopen(FName, "rb")) {
  79.     fclose(FilePtr);
  80.     PrintMsg(MSG_OUTFEX_S, FName);
  81.     gets(Answer);
  82.     if (toupper(Answer[0]) != 'Y') exit(0);
  83.   }
  84. }
  85.  
  86. /*************************************************************************/
  87.  
  88. static void CloseFile(FILE **File, char **FName)
  89. {
  90.   if (*File) {
  91.     if (EOF == fclose(*File)) {
  92.       PrintMsg(ERR_CLOSE_S, *FName);
  93.     }
  94.     *File = NULL;
  95. #ifdef AMIGA
  96.     if (!strcmp(*FName, TEMPFILENAME))
  97.       if (remove(*FName)) PrintMsg(ERR_IFFREM_S, *FName);
  98. #endif
  99.     *FName = NULLSTR;
  100.   }
  101. }
  102.  
  103. /*************************************************************************/
  104.  
  105. extern void CloseFiles(gvType *gv)
  106. {
  107.   CloseFile(&gv->Inputfile, &gv->Options.InputfileName);
  108.   CloseFile(&gv->Outputfile, &gv->Options.OutputfileName);
  109. }
  110.